package com.lyrcsoft.rxjava1x;

import com.lyrcsoft.rxjava1x.dao.DataUtils;
import com.lyrcsoft.rxjava1x.entity.Community;
import com.lyrcsoft.rxjava1x.entity.House;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fujc2dev@126.com
 * @date 2024-11-15
 */
public class Rx1Demo2 {

    public static void main(String[] args) {
        //  from 创建是安全的集合。
        Observable.from(DataUtils.getCommunityList())
                .flatMap(new Func1<Community, Observable<House>>() {
                    @Override
                    public Observable<House> call(Community community) {
                        return Observable.from(community.getHouses());
                    }
                })
                .filter(new Func1<House, Boolean>() {
                    @Override
                    public Boolean call(House o) {
                        return o.getPrice() >= 50000;
                    }
                })
                .subscribe(new Action1<House>() {
                    @Override
                    public void call(House house) {
                        addHouseInformationToScreen(house);
                    }
                });
    }

    private static void addHouseInformationToScreen(House house) {
        // do anything
        System.out.println(house);
    }

    private static void runOnUiThread(Runnable runnable) {
        // do anything
    }



    private static void demo1() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                //从服务端获取小区列表
                List<Community> communities = DataUtils.getCommunityList();
                for (Community community : communities) {
                    List<House> houses = community.getHouses();
                    for (House house : houses) {
                        if (house.getPrice() >= 5000000) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //将房子的信息添加到屏幕上
                                    addHouseInformationToScreen(house);
                                }


                            });
                        }
                    }
                }
            }

        }.start();
    }
}
